package link.jfire.disqueue;

import link.jfire.baseutil.verify.Verify;

public class RingBuffer<T>
{
    private Object[]    entries;
    private final int   size;
    private Sequencer   sequencer;
    private Sequencer[] entryHandlerSequencers;
    
    public RingBuffer(int size, EntryFactory<T> entryFactory)
    {
        Verify.True(size > 1, "大小必须大于1");
        Verify.True(Integer.bitCount(size) == 1, "大小必须是2的整数次方幂");
        this.size = size;
        entries = new Object[size];
        // 事先填充，避免gc操作
        for (int i = 0; i < size; i++)
        {
            entries[i] = entryFactory.newInstance();
        }
    }
    
    public long getCursor()
    {
        return sequencer.getCursor();
    }
    
    public Sequencer getSequencer()
    {
        return sequencer;
    }
    
    public Sequencer[] getEntryHandlerSequencers()
    {
        return entryHandlerSequencers;
    }
    
    public int size()
    {
        return size;
    }
    
}
